import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMessageBox, QPushButton
from random import randint


class Demo(QWidget):
    distance_from_center = 0

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(350, 350, 300, 250)
        self.setWindowTitle('SignalAndSlot5')
        
        rock = QPushButton('rock', self)
        scissors = QPushButton('scissors', self)
        paper = QPushButton('paper', self)
        
        rock.setGeometry(25,80,60,50)
        scissors.setGeometry(120,80,60,50)
        paper.setGeometry(215,80,60,50)
        
        rock.clicked.connect(self.buttonclicked)
        scissors.clicked.connect(self.buttonclicked)
        paper.clicked.connect(self.buttonclicked)
        
        self.show()
        
        
    def buttonclicked(self, event): 
        computer = randint(1, 3)            # 产生一个随机数，当石头剪刀布
        player = 0
        sender = self.sender()      # 记录sender
        if sender.text() == 'rock':     # 判断sender，用于判断石头剪刀布
            player = 1
        elif sender.text() == 'scissors':
            player = 2
        else:
            player = 3
            
        if player == computer:
            QMessageBox.about(self, 'result', 'draw')
        elif player == 1 and computer == 2:
            QMessageBox.about(self, 'result', 'you win')
        elif player == 1 and computer == 3:
            QMessageBox.about(self, 'result', 'you lose')    
        elif player == 2 and computer == 1:
            QMessageBox.about(self, 'result', 'you lose')
        elif player == 2 and computer == 3:
            QMessageBox.about(self, 'result', 'you win')
        elif player == 3 and computer == 1:
            QMessageBox.about(self, 'result', 'you win')
        elif player == 3 and computer == 2:
            QMessageBox.about(self, 'result', 'you lose')
        

if __name__ == '__main__':
    app = QApplication(sys.argv)
    demo = Demo()
    sys.exit(app.exec_())
    